set more off
clear all

global BHHDIR Dropbox\INE\data\bhh
global CHDIR Dropbox\INE\data\stata

cap log close
log using an_enia_reg_level, t replace

*** runs OLS regs of performance on IP - for tables of level and fe regressions
*** Also generates Table B-4
*** sample provided by BHH on 16 Oct 2019

use sample_clean, clear

keep id year indold logs logm loga loge tfp* dip dpat dtm dtfp dloge dlogs dlogm dloga ///
     foreign mixed public indiv export santiago hhi share4 shfor

qui tab indold, gen(i_)
global inddum i_2-i_18
qui tab year, gen(y_)
global yrdum y_3-y_10
global dummies $yrdum $inddum

gen logae = loga-loge
gen logse = logs-loge
gen logme = logm-loge
gen dexport = exports>0
gen loghhi = log(hhi)
gen logms = log(share4)
gen logfs = log(shfor)

gen mislogfs = (logfs==.)
replace logfs = 0 if logfs==.

do $CHDIR\cleanfile

global vars foreign public indiv dexport santiago logms loghhi logfs mislogfs

sort id year
tabstat loge logse logae logme logms loghhi logfs, stat(mean sd median p25 p75 min max) col(stat)
sum dpat dtm foreign public indiv dexport santiago mislogfs

gen dpatl1 = dpat[_n-1] if id==id[_n-1]
gen dpatl2 = dpat[_n-2] if id==id[_n-2]
gen dtml1 = dtm[_n-1] if id==id[_n-1]
gen dtml2 = dtm[_n-2] if id==id[_n-2]
keep if id==id[_n-2]  /* drop the first 2 lagged obs to keep sample the same for all regs*/

tabstat loge logse logae logme logms loghhi logfs, stat(mean sd median p25 p75 min max) col(stat)
sum dpat dtm foreign public indiv dexport santiago mislogfs

**** patenting
reg logse loge logae logme     $yrdum, cluster(id)
est store patres0
reg logse loge logae logme dpat $yrdum, cluster(id)
est store patres1
reg logse loge logae logme dpat $vars $yrdum, cluster(id)
est store patres2

reg logse loge logae logme dpat dpatl1 dpatl2 $vars $yrdum, cluster(id)
est store patres7
reg logse loge logae logme dpat dpatl1 dpatl2 $vars $dummies, cluster(id)
est store patres8
lincom dpat+dpatl1+dpatl2

esttab patres*, keep(loge logae logme dpat dpatl1 dpatl2 $vars)       ///
                order(dpat dpatl1 dpatl2 loge logae logme $vars)      ///
         b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust)  ///
		 nogaps nolines compress

**** trademarking
reg logse loge logae logme     $yrdum, cluster(id)
est store tmres0
reg logse loge logae logme dtm $yrdum, cluster(id)
est store tmres1
reg logse loge logae logme dtm $vars $yrdum, cluster(id)
est store tmres2

reg logse loge logae logme dtm dtml1 dtml2 $vars $yrdum, cluster(id)
est store tmres7
reg logse loge logae logme dtm dtml1 dtml2 $vars $dummies, cluster(id)
est store tmres8
lincom dtm+dtml1+dtml2

esttab tmres*, keep(loge logae logme dtm dtml1 dtml2 $vars)           ///
               order(dtm dtml1 dtml2 loge logae logme $vars)          ///
         b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust)  ///
		 nogaps nolines compress

log close
		 